<template>
  <div>
    <div
      style="
        float: left;
        background-color: white;
        width: 50px;
        max-height: 450px;
        min-height: 420px;
      "
    ></div>

    <div
      align="left"
      style="
        float: left;
        background-color: white;
        width: 300px;
        max-height: 450px;
        min-height: 420px;
        line-height: 28px;
        margin-top: 10px;
      "
    >
      <el-form
        ref="form"
        :model="ruleForm"
        :rules="rules"
        label-width="80px"
        status-icon
      >
        <el-form-item label="旧密码" prop="oldPass" label-width="auto">
          <el-input type="password" v-model="ruleForm.oldPass"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass" label-width="auto">
          <el-input type="password" v-model="ruleForm.newPass"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="againPass" label-width="auto">
          <el-input type="password" v-model="ruleForm.againPass"></el-input>
        </el-form-item>
        <el-form-item label-width="auto">
          <el-button type="primary" @click="submitForm">修改密码</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    var validateAgainPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.newPass) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    var validateNewPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value === this.ruleForm.realPass) {
        callback(new Error("新密码不能与旧密码一致!"));
      } else {
        callback();
      }
    };
    var validateOldPass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.realPass) {
        callback(new Error("旧密码输入错误!"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        realPass: JSON.parse(sessionStorage.getItem("user")).password,
        oldPass: "",
        newPass: "",
        againPass: "",
      },
      rules: {
        oldPass: [
          { required: true, message: "请输入旧密码", trigger: "blur" },
          { validator: validateOldPass, trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        newPass: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          { validator: validateNewPass, trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
        againPass: [
          { required: true, message: "请重新输入密码", trigger: "blur" },
          { validator: validateAgainPass, trigger: "blur" },
          {
            min: 6,
            max: 16,
            message: "长度在 6 到 16 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      let uId = JSON.parse(sessionStorage.getItem("user")).uId;
      let password = this.ruleForm.newPass;
      var _this = this;
      $.ajax({
        type: "post",
        data: { uId, password },
        url: "http://localhost:8080/forum/user/updatepass",
        dataType: "json",
        success: function (response) {
          console.log(response);
          if (response === 200) {
            // // 更新sessionStorage中的值
            // let oldUser = JSON.parse(sessionStorage.getItem("user"));
            // let newUser = {
            //   uId: oldUser.uId,
            //   username: oldUser.username,
            //   // password是新的
            //   password: password,
            //   nickname: oldUser.nickname,
            //   name: oldUser.name,
            //   sex: oldUser.sex,
            //   avatar: oldUser.avatar,
            //   phone: oldUser.phone,
            //   idNumber: oldUser.idNumber,
            //   enterYear: oldUser.enterYear,
            //   className: oldUser.className,
            //   subject: oldUser.subject,
            //   email: oldUser.email,
            //   messageCount: oldUser.messageCount,
            //   topicCount: oldUser.topicCount,
            //   commentCount: oldUser.commentCount,
            // };
            // sessionStorage.setItem("user", JSON.stringify(newUser));
            sessionStorage.removeItem("user");
            _this.$message({
              message: "修改成功，请重新登录",
              type: "success",
              duration: 1000,
            });
            _this.$router.push("/Home/Login");
          }
        },
      });
    },
  },
};
</script>

<style scoped>
#home {
  background-color: white;
  width: 778px;
  height: 360px;
  font-size: 14px;
  font-family: 微软雅黑;
  font-size: 14px;
  margin: auto;
}

.inputText {
  width: 250px;
  height: 26px;
  font-size: 16px;
}

.butt {
  background-color: #6699cc;
  width: 100px;
  height: 30px;
  border: 0;
  color: white;
  font-size: 18px;
}

.butt:hover {
  background-color: #71aae3;
  border: 0;
}

table tr {
  height: 30px;
  line-height: 40px;
}
</style>